#define  _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"

void QueueInit(pQueue pq)
{
	assert(pq);

	pq->phead = pq->ptail = NULL;
}


pQNode BuyQueueNode(QDataType x)
{
	pQNode newnode = (pQNode)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->val = x;
	newnode->next = NULL;

	return newnode;
}


void QueuePush(pQueue pq, QDataType x)
{
	assert(pq);
	
	pQNode newnode = BuyQueueNode(x);
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
}


void QueuePop(pQueue pq)
{
	assert(pq && pq->phead);

	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		pQNode next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
}


QDataType QueueFront(pQueue pq)
{
	assert(pq && pq->phead);

	return pq->phead->val;
}


QDataType QueueBack(pQueue pq)
{
	assert(pq->phead && pq);

	return pq->ptail->val;
}


bool QueueEmpty(pQueue pq)
{
	assert(pq);

	return pq->phead == NULL;
}